<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form', '', 0);
pc_base::load_app_func('util');
pc_base::load_sys_class('format','',0);
class contact extends admin {
    public function __construct() {
        parent::__construct();
        $this->db = pc_base::load_model('contact_model');
    }

    public function init(){
       $siteid= param::get_cookie('siteid');
        $data=$this->db->select();
        include $this->admin_tpl('contact');

    }

    public function add(){
        include $this->admin_tpl('contact_add');
    }
    public function action(){
        $siteid= param::get_cookie('siteid');
        if($_POST['act']=='add'){
            $ty=explode('.',$_FILES['dd']['name']);
            $type=$ty[1];
            $file=$_FILES['dd']['tmp_name']; 
            $new="statics/images/contact/".time().".$type";
            if(!empty($file)){
               if(move_uploaded_file($file,$new)){
                   $data['title']=$_POST['title'];
                   $data['src']=$new;
                   $data['url']=$_POST['url'];
                   $data['listid']=$_POST['listid'];
                   $data['posid']=$_POST['posid'];
                   $data['addtime']=time();
                   $data['siteid']=$siteid;
                   if($this->db->insert($data)){
                       showmessage("添加成功","index.php?m=webman&c=contact&a=init");
                   }else{
                       showmessage("添加失败，请稍后再试","index.php?m=webman&c=contact&a=init");
                   }
               }else{
                   showmessage("图片上传失败，请稍后再试","index.php?m=webman&c=contact&a=init");
               };
            }else{
                showmessage("请先选择图片","index.php?m=webman&c=contact&a=init");
            }

            exit;
        }


       if($_POST['act']=='edit'){
		   // echo 2;die;
           $id=$_POST['id'];
           $data['url']=$_POST['url'];
           $data['posid']=$_POST['posid'];
           $data['src']=$_POST['src'];
           $data['title']=$_POST['title'];
           $data['listid']=$_POST['listid'];
           $data['addtime']=time();
           $data['siteid']=$siteid;
           if(empty($_FILES['dd']['tmp_name'])){
                if($this->db->update($data,"id=$id")){
                    showmessage("修改成功","index.php?m=webman&c=contact&a=init");
                }else{
                    showmessage("修改失败","index.php?m=webman&c=contact&a=init");
                }
           }else{
               unlink($data['src']);
               $ty=explode('.',$_FILES['dd']['name']);
               $type=$ty[1];
               $file=$_FILES['dd']['tmp_name'];
               $data['src']="statics/images/contact/".time().".$type";
               if(move_uploaded_file($file,$data['src'])){
                   if($this->db->update($data,"id=$id")){
                       showmessage("修改成功","index.php?m=webman&c=contact&a=init");
                   }else{
                       showmessage("修改失败","index.php?m=webman&c=contact&a=init");
                   }
               }else{
                   showmessage("图片上传失败","index.php?m=webman&c=contact&a=init");
               }
           }

           exit;
       }
    }

    public function del(){
        $id=$_POST['id'];
        $data=$this->db->get_one("id=$id","src");
        if($this->db->delete("id=$id")){
            unlink($data['src']);
            exit("ok");
        }else{
            exit("fail");
        }
    }

    public function edit(){
		$id=$_POST['id'];
		if($_POST['erweima']!=$_POST['erweima2']){
			unlink($_POST['erweima2']);
		}
		unset($_POST['erweima2']);
		unset($_POST['id']);
		unset($_POST['pc_hash']);
        $data=$this->db->update($_POST,"id=$id");
		if($data){
			showmessage("修改成功","index.php?m=webman&c=contact&a=init");
		}else{
			showmessage("修改失败，请稍后再试","index.php?m=webman&c=contact&a=init");
		}
    }
	
	public function filess(){
		
		if($_FILES){
			$ty=explode('.',$_FILES['upload_file0']['name']);
            $type=$ty[1];
            $file=$_FILES['upload_file0']['tmp_name']; 
			$dir="statics/images/contact/";
            $new="statics/images/contact/".time().".$type";
			@mkdir($dir,0777,true);
			if(move_uploaded_file($file,$new)){
				echo $new;die;
			}else{
				echo 0;die;
			}
		}else{
			echo 2;die;
		}
	}


}